package com.smartao.base.position;

import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.smartao.base.Constant;
import com.smartao.base.department.Department;
import com.smartao.base.department.DepartmentService;
import com.smartao.base.employee.Employee;
import com.smartao.base.employee.EmployeeService;
import com.smartao.base.selectitem.Selectitem;
import com.smartao.base.selectitem.SelectitemService;
import com.smartao.core.mvc.BaseAction;

@Controller
@Scope("prototype")
@Results({ @Result(name = "json", type="json", params={"root","departments"}), 
		@Result(name = "jsonSuccess", type="json", params={"root","jsonMap"}),
		@Result(name = "jsonFailure", type="json", params={"root","jsonMap"}),
		@Result(name = "gotoList", type="redirect", location="position.action"),
		@Result(name = "update", location="update.jsp"),
		@Result(name = "addUserToPosition", location="addUserToPosition.jsp"),
		@Result(name = "view", location="view.jsp")})
public class PositionAction extends BaseAction{

	private static final long serialVersionUID = -3467828480680420115L;
	
	@Autowired
	private DepartmentService departmentService;
	
	@Autowired
	private EmployeeService employeeService;
	
	@Autowired
	private SelectitemService selectitemService;

	@Autowired
	private PositionService positionService;
	
	private Position position;
	
	private List<Selectitem> positionRanks;
	
	private List<Position> positions;
	
	private List<Employee> employees;
	
	private Long employeeId;
	
	private Long[] employeeIds;
	
	private Long positionId;
	
	private Long departmentId;
	
	private String message;
	
	@Override
	public String execute(){
		positions = positionService.findAll();
		return SUCCESS;
	}
	
	public String preUpdate(){
		// 新增
		if(id==null){
			position = new Position();
			if(departmentId!=null){
				Department department = departmentService.findOne(departmentId);
				position.setDepartment(department);
			}
		}
		// 更新
		else{
			position = positionService.findOne(id);
		}
		positionRanks = selectitemService.findBySelecttypeCode(Constant.SELECTTYPE_POSITION_RANK);
		
		return "update";
	}
	
	public String update(){
		if(position.getId()!=null){
			Position p = positionService.findOne(position.getId());
			position.setEmployees(p.getEmployees());
		}
		positionService.save(position);
		positionRanks = selectitemService.findBySelecttypeCode(Constant.SELECTTYPE_POSITION_RANK);
		message = "保存成功.";
		return "update";
	}
	
	public String delete(){
		positionService.delete(id);
		return "gotoList";
	}

	public String view(){
		position = positionService.findPositionAndEmployees(id);
		return "view";
	}
	
	public String preAddEmployeeToPosition(){
		position = positionService.findOne(id);
		//TODO 岗位上添加用户
		//employees = employeeService.findAll();
		return "addUserToPosition";
	}
	
	public String addEmployeeToPosition(){
		position = positionService.findPositionAndEmployees(id);
		List<Employee> employees = employeeService.findByIds(employeeIds);
		for(Employee em : employees){
			position.addUser(em);
		}
		positionService.save(position);
		return "view";
	}
	
	public String removeEmployeeFromPosition(){
		position = positionService.findPositionAndEmployees(id);
		for(Employee em : position.getEmployees()){
			if(em.getId().equals(employeeId)){
				position.getEmployees().remove(em);
				break;
			}
		}
		positionService.save(position);
		return "view";
	}
	
	public String listPositionRank(){
		positionRanks = selectitemService.findBySelecttypeCode(Constant.SELECTTYPE_POSITION_RANK);
		return "listPositionRank";
	}
	
	public Position getPosition() {
		return position;
	}

	public void setPosition(Position position) {
		this.position = position;
	}

	public List<Position> getPositions() {
		return positions;
	}

	public void setPositions(List<Position> positions) {
		this.positions = positions;
	}

	public Long getPositionId() {
		return positionId;
	}

	public void setPositionId(Long positionId) {
		this.positionId = positionId;
	}

	public Long getDepartmentId() {
		return departmentId;
	}

	public void setDepartmentId(Long departmentId) {
		this.departmentId = departmentId;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public List<Selectitem> getPositionRanks() {
		return positionRanks;
	}

	public void setPositionRanks(List<Selectitem> positionRanks) {
		this.positionRanks = positionRanks;
	}

	public List<Employee> getEmployees() {
		return employees;
	}

	public void setEmployees(List<Employee> employees) {
		this.employees = employees;
	}

	public Long[] getEmployeeIds() {
		return employeeIds;
	}

	public void setEmployeeIds(Long[] employeeIds) {
		this.employeeIds = employeeIds;
	}

	public Long getEmployeeId() {
		return employeeId;
	}

	public void setEmployeeId(Long employeeId) {
		this.employeeId = employeeId;
	}
	
}
